package com.nuoniu.sibanyun.mapper.erp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.erp.ErpPeriod;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
public interface ErpPeriodMapper extends BaseMapper<ErpPeriod> {

    @Select("SELECT max(current_year) FROM fab_period WHERE company_id = #{companyId}")
    Integer getLastYear(Integer companyId);

    @Select("select * from fab_period where begin_date <= #{businessDate} and end_date >= #{businessDate} and company_id=#{companyId} and account_id=#{accountId}")
    ErpPeriod getByDate(LocalDate businessDate, Integer companyId, Integer accountId);

    @Update("update fab_period set fi_terminal_state=#{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void settleAccounts(Integer year, Integer month, Integer status, Integer companyId);

    @Update("update fab_period set in_terminal_state=#{status} ,pu_terminal_state = #{status},sa_terminal_state = #{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void BusinessSettleAccounts(Integer year, Integer month, Integer status, Integer companyId);
    /**
     * 根据日期 获取 期间
     * @param companyId
     * @param now
     * @return
     */
    @Select("SELECT * FROM fab_period WHERE company_id = #{companyId} AND date_format( end_date,'%y-%m-%d')  >= #{now} AND date_format( begin_date,'%y-%m-%d')  <= #{now} ")
    ErpPeriod getByDateStr(Integer companyId,String now);

    /**
     * 存货结账
     * @author 小啊刚
     * @date 2021-07-15
     */
    @Update("update fab_period set inventory_terminal_state=#{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void inventorySettleAccounts(Integer year, Integer month, Integer status, Integer companyId);

    /**
     * 获取某期间
     * @author 小啊刚
     * @date 2021-07-17
     */
    @Select("select * from fab_period where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    ErpPeriod getDetail(Integer year, Integer month, Integer companyId);

    /**
     * 采购结账
     * @author 小啊刚
     * @date 2021-07-17
     */
    @Update("update fab_period set pu_terminal_state=#{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void updatePuTerminalState(Integer year, Integer month, Integer status, Integer companyId);

    /**
     * 销售结账
     * @author 小啊刚
     * @date 2021-07-17
     */
    @Update("update fab_period set sa_terminal_state=#{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void updateSaTerminalState(Integer year, Integer month, Integer status, Integer companyId);

    /**
     * 库存结账
     * @author 小啊刚
     * @date 2021-07-17
     */
    @Update("update fab_period set in_terminal_state=#{status} where current_year=#{year} and current_period=#{month} and company_id=#{companyId}")
    void updateInTerminalState(Integer year, Integer month, Integer status, Integer companyId);
}
